/*
 * Vectrics 
 * Created on Jan 29, 2004
 *
 */
package org.vectrics.imaging.client;

import org.apache.log4j.Logger;
import org.vectrics.client.module.ModulePanel;
import org.vectrics.swing.GuiManager;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;

/**
 * @author mmoore
 */
public class ImagingPanel extends ModulePanel implements MouseListener, ComponentListener, ActionListener {
	private transient static Logger log = Logger.getLogger(ImagingPanel.class);
	private String directory = "E:/data/documents/art";
	private int thumbHeight = 60;
	private ImagePanel imagePanel = new ImagePanel();
	private JFileChooser dirChooser = null;
	private JPanel thumbsPanel = null;
	private JComboBox scaleCombo = null;
	private JScrollPane imageScrollPane = null;
	
	/**
	 * 
	 */
	public ImagingPanel() {
		super();
		initComponents();
		
	}

	protected void encryptButtonPressed(ActionEvent e) {
		File selectedDir = dirChooser.getSelectedFile();
		if (selectedDir != null) {
			boolean recurse = true;
			EncryptedFileUtils.encryptDirectoryImages(selectedDir.getPath(), "/data", recurse);
		}
	}	

	public void actionPerformed(ActionEvent event) {
		log.info("actionPerformed() - source class = " + event.getSource().getClass().getName());
		if (event.getSource() == this.scaleCombo) {
			String sizeStr = (String)scaleCombo.getSelectedItem();
			log.info("New scale = " + sizeStr);
			File selectedDir = dirChooser.getSelectedFile();
			showImage(selectedDir.getPath());
		}
		else if (event.getSource() == this.dirChooser) {
			File selectedDir = dirChooser.getSelectedFile();
			if (selectedDir != null) {
				this.showDirectoryImages(thumbsPanel, selectedDir.getPath());
				thumbsPanel.repaint();
				thumbsPanel.validate();
				
			}
		}
	}
	

	private void showImage(String fileName) {
		log.info("showImage - file: " + fileName);
		String scaleStr = (String)scaleCombo.getSelectedItem();
		log.info("Scale = " + scaleStr);
		int scale = Integer.parseInt(scaleStr);
		
		imagePanel.setScale(scale);
		imagePanel.showFile(fileName);
		imagePanel.repaint();
		imagePanel.validate();
		imageScrollPane.validate();		
	}

	
	public void componentHidden(ComponentEvent event) {}
	public void componentShown(ComponentEvent event) {}
	public void componentMoved(ComponentEvent event) {}
	public void componentResized(ComponentEvent event) {
		log.info("componentResized() - ");
		thumbsPanel.repaint();
	}
	
	public void mouseClicked(MouseEvent event) {
		if (event.getSource() instanceof ImagePanel) {
			ImagePanel selectedPanel = (ImagePanel)event.getSource();
			log.info("Selected image: " + selectedPanel.getFileName());
			showImage(selectedPanel.getFileName());
		}
	}
	
	public void mouseReleased(MouseEvent event) {			}
	public void mousePressed(MouseEvent event) {	}
	public void mouseEntered(MouseEvent event) {	}
	public void mouseExited(MouseEvent event) {	}
	
	private void initComponents() {
		JSplitPane mainSplitPane = new JSplitPane();
		mainSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(mainSplitPane);

		JPanel topPanel = new JPanel();
		topPanel.setBackground(Color.PINK);
		JPanel imageHolderPanel = new JPanel();
		imageHolderPanel.setLayout(new BorderLayout());
		JPanel imageControlPanel = new JPanel();
		imageControlPanel.setLayout(new BoxLayout(imageControlPanel, BoxLayout.X_AXIS));
		
		log.debug("Setting up scale combo box");
		scaleCombo = new JComboBox();
		scaleCombo.addItem("50");
		scaleCombo.addItem("100");
		scaleCombo.addItem("150");
		scaleCombo.addActionListener(this);
		imageControlPanel.add(scaleCombo);
		
		JButton encryptButton = GuiManager.getInstance().createButton("Encrypt Dir", 90, 20);
		encryptButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(ActionEvent e) {
					encryptButtonPressed(e);
				}
			});

		imageControlPanel.add(encryptButton);
		
		
		log.debug("Setting imageControlPanel Size");
		imageControlPanel.setMaximumSize(new Dimension(4000, 20));
		topPanel.setLayout(new BorderLayout());

		imageScrollPane = new JScrollPane();
		imageScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		imageScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		//this.add(scrollPane, BorderLayout.NORTH);
		
		showImage("E:\\data\\test.jpg");
		topPanel.add(imageScrollPane, BorderLayout.CENTER);
		topPanel.add(imageControlPanel, BorderLayout.SOUTH);
		imageHolderPanel.add(new JPanel(), BorderLayout.NORTH);
		imageHolderPanel.add(new JPanel(), BorderLayout.SOUTH);
		imageHolderPanel.add(new JPanel(), BorderLayout.EAST);
		imageHolderPanel.add(new JPanel(), BorderLayout.WEST);
		imageHolderPanel.add(imagePanel, BorderLayout.CENTER);
		imageHolderPanel.setBackground(Color.black);
		imagePanel.setBorder(new EtchedBorder());
		imageScrollPane.getViewport().add(imageHolderPanel);
		imageScrollPane.getViewport().setBackground(Color.BLUE);
		mainSplitPane.add(topPanel, JSplitPane.TOP);
		mainSplitPane.addComponentListener(this);
		JScrollPane bottomScrollPane = new JScrollPane();
		 
		mainSplitPane.add(bottomScrollPane, JSplitPane.BOTTOM);
		JPanel bottomPanel = new JPanel();
		bottomPanel.addComponentListener(this);
		bottomScrollPane.getViewport().add(bottomPanel);
		bottomScrollPane.getViewport().setBackground(Color.BLACK);
		bottomScrollPane.getViewport().setLayout(new BoxLayout(bottomScrollPane.getViewport(), BoxLayout.X_AXIS));
		
		bottomPanel.setBackground(Color.CYAN);
		bottomPanel.setMinimumSize(new Dimension(100, 10));
		
		
		thumbsPanel = new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
		JPanel directorySelectPanel = new JPanel();
		bottomPanel.add(directorySelectPanel);
		
		
		dirChooser = new JFileChooser("G:/");
		dirChooser.setMaximumSize(new Dimension(200, 4000));
		dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		dirChooser.addActionListener(this);
		directorySelectPanel.setLayout(new BoxLayout(directorySelectPanel, BoxLayout.X_AXIS));
		directorySelectPanel.add(dirChooser);
		
		bottomPanel.add(thumbsPanel);
		
		//showDirectoryImages(thumbsPanel, directory);
				
	}
	
	void showDirectoryImages(JPanel parentPanel, String directoryName) {
		//parentPanel.setMinimumSize(new Dimension(100, 10));
		log.debug("showDirectoryImages()");
		int imagesPerRow = 6;
				
		parentPanel.removeAll();
		
		
		DirectoryManager dirMgr = new DirectoryManager();
		dirMgr.clearFileExtensions();
		dirMgr.addFileExtension(".jpg");
		dirMgr.addFileExtension(".eji");
		dirMgr.setDirectory(directoryName);

		
		Vector files = dirMgr.findFileSet();
		
		int rowCount = files.size() / imagesPerRow;
		parentPanel.setLayout(new GridLayout(rowCount, imagesPerRow));
		
		Iterator iter = files.iterator();
		while (iter.hasNext()) {
			String file = (String)iter.next();
			ImagePanel imagePanel = new ImagePanel();
			imagePanel.setFixedHeight(thumbHeight);
			imagePanel.showFile(directoryName + "/" + file);
			//imagePanel.setMinimumSize(new Dimension(10, thumbHeight));
			imagePanel.setBorder(new LineBorder(Color.DARK_GRAY, 1));
			imagePanel.setBackground(Color.MAGENTA);
			imagePanel.addMouseListener(this);
			parentPanel.add(imagePanel);
		}
		
		
	}	
	
		

}
